<template>
  <!-- <van-nav-bar title="标题" left-text="返回" left-arrow @click-left="onClickLeft" /> -->
  <div class="box">
    <div class="kong" v-if="cart.length == 0">购物车为空</div>
    <div class="box2" v-else>
      <div class="cards" v-for="item in cart" :key="item.shop_spu_id">
        <van-swipe-cell>
          <div class="card-box">
            <input
              type="checkbox"
              @click="upChecked(item.shop_spu_id)"
              :checked="item.checked"
            />
            <div class="card">
              <img :src="item.swiper_info.img_list[0]" alt="" />
              <div class="card-info">
                <p>{{ item.drug_desc.name }}</p>
                <p style="color: red; font-weight: 600">
                  ￥{{ item.goods_info.price }}
                </p>
              </div>
            </div>
            <div class="operate">
              <button @click="add(item.shop_spu_id)">+</button>
              <p>{{ item.count }}</p>
              <button @click="sub(item.shop_spu_id)">-</button>
            </div>
          </div>
          <template #right>
            <van-button
              square
              text="删除"
              type="danger"
              class="delete-button"
              @click="deleteCart(item.shop_spu_id)"
            />
          </template>
        </van-swipe-cell>
      </div>
    </div>
    <van-submit-bar
      :price="allPrices"
      button-text="提交订单"
      class="submit"
      v-show="cart.length != 0"
      @submit="onSubmit"
    >
      <van-checkbox v-model="checked" @click="allChecked">全选</van-checkbox>
    </van-submit-bar>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import axios from 'axios'
import { useCounterStore } from '../../api/pinia'
// const onClickLeft = () => history.back();
const counterStore = useCounterStore()
const cart = ref('')
const checked = ref()
const allPrices = ref()
const getData = () => {
  cart.value = JSON.parse(localStorage.getItem('cart')) || []
  checked.value = counterStore.allCheckeds
  allPrices.value = counterStore.allPrices
}
getData()
// 单选选中
const upChecked = (index: number) => {
  counterStore.Dchecked(index)
  counterStore.checkAllSelectedStatus()
  counterStore.allPrice()
  getData()
}
// 全选
const allChecked = () => {
  counterStore.allChecked()
  counterStore.allPrice()
  getData()
}
// 加
const add = index => {
  counterStore.addCount(index)
  counterStore.allPrice()
  getData()
}
// 减
const sub = index => {
  counterStore.subCount(index)
  counterStore.allPrice()
  getData()
}

// 删除
const deleteCart = index => {
  counterStore.deleteCart(index)
  counterStore.allPrice()
  getData()
}
// 提交订单
const onSubmit = async () => {
  console.log(allPrices.value / 100)

  console.log('提交订单')
  await axios
    .get('/api/api/pay', {
      params: {
        id: new Date().getTime().toString(),
        title: '药品',
        price: allPrices.value,
      },
    })
    .then(res => {
      console.log(res)

      if (res.data.code === 200) {
        window.open(res.data.result)
      }
    })
}
</script>
<style scoped>
.box {
  width: 100%;
  height: 100vh;
}

.kong {
  width: 100%;
  height: 10%;
  text-align: center;
  line-height: 10vh;
  font-size: 20px;
  color: #ccc;
}

.card {
  width: 100%;
  height: 100px;
  display: flex;
  padding: 10px;
}

.card img {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  margin-bottom: 25px;
}

.cards {
  width: 100%;
  height: 100px;
  display: flex;
  flex-direction: row;
}
.card-box {
  width: 100%;
  height: 100px;
  display: flex;
  flex-direction: row;
}
.box2 {
  width: 100%;
  padding: 5px;
  background: white;
}

.submit {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 100px;
}

.card-info {
  width: 50%;
  line-height: 30px;
}

.operate {
  width: 20%;
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.operate button {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 1px solid #ccc;
  background: #ccc;
  color: white;
  font-weight: 600;
  cursor: pointer;
}
.van-swipe-cell {
  width: 100%;
}
.van-button {
  height: 100%;
  margin-left: 20px;
}
</style>